package com.android.signhelper.presenter;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;

import com.android.signhelper.base.BasePresenter;
import com.android.signhelper.bean.SignInfo;
import com.android.signhelper.bean.Student;
import com.android.signhelper.interfaces.MainContract;
import com.android.signhelper.interfaces.MyCallback;
import com.android.signhelper.jsonbean.login.Login;
import com.android.signhelper.model.MainModel;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Created by ikata on 2018/11/14.
 */

public class MainPresenter extends BasePresenter<MainContract.View> implements MainContract.Presenter{
    private MainModel model;

    public MainPresenter()
    {
      model=new MainModel();
    }

    @Override
    public void sign(final String token, final SignInfo signInfo) {
        Map<String,String> map= new HashMap<>();
        map.put("devUuid", signInfo.getDevUuid());
        map.put("signId", signInfo.getSignId());
        map.put("bleId", signInfo.getBleId());
        model.sign(token,map, new MyCallback() {
           @Override
           public void onSuccess(@Nullable Bundle bundle) {
               updateData(token);
           }
           @Override
           public void onFailed(String msg) {
               if (getView() != null) {
                   getView().showToast(msg);
               }
           }
       });
    }

    @Override
    public void login(final String userName, final String password) {
        model.login(userName, password, new MyCallback() {
            @Override
            public void onSuccess(@Nullable Bundle bundle) {
                Login login = bundle.getParcelable("data");
                if (getView() != null) {
                    if (login.getResult().getIsSuccess().equals("1")){
                        //登录成功
                        updateData(login.getResult().getToken());
                        //检查是否可签到
                        Log.d("token", login.getResult().getToken() + "--");
                        detail(login.getResult().getToken(),userName);
                    }
                }
            }
            @Override
            public void onFailed(String msg) {
                if (getView()!=null)
                {
                    getView().showToast(msg);
                }
            }
        });
    }

    @Override
    public void detail(final String token, final String userName) {
      model.detail(token, new MyCallback() {
          @Override
          public void onSuccess(@Nullable Bundle bundle) {
              updateData(token);
          }

          @Override
          public void onFailed(String msg) {
              if (getView()!=null)
              {
                  getView().showToast(msg);
              }
          }
      });
    }

    @Override
    public void initData() {
                if (getView()!=null)
                {
                    getView().showData(model.getMyDatabase().queryAll());
                }

    }

    @Override
    public void updateData(String token) {
        if (getView() != null) {
            getView().showData(model.queryData(token));
        }
    }

    @Override
    public void sign(List<Student> list) {
        for (Student student : list) {
            final SignInfo signInfo = student.getSignInfo();
            sign(student.getToken(), signInfo);
        }
        if (getView() != null) {
            getView().onCompleted();
        }
    }

    @Override
    public void unBinderView() {
        super.unBinderView();
        //解除订阅
        model.getCompositeDisposable().dispose();
    }
}
